package com.geek.novel.activity.auth

import android.os.Bundle
import android.view.View
import com.geek.novel.R
import com.geek.novel.activity.auth.view.listener.PwdViewerClickListener
import com.geek.novel.activity.base.TitleBarActivity
import com.geek.novel.bo.AppContext
import com.geek.novel.common.Constant
import com.geek.novel.error.ValidateException
import com.geek.novel.listener.ApiResultListener
import com.geek.novel.services.manager.AuthManager
import com.geek.novel.utils.SecurityUtil
import com.geek.novel.utils.ToastUtils
import com.geek.novel.utils.ValidateUtil
import com.geek.novel.vo.ApiResult
import kotlinx.android.synthetic.main.activity_modify_password.*

/**
 * 修改密码页面
 */
class ModifyPasswordActivity: TitleBarActivity(), ApiResultListener {

	//用户信息管理器
	private lateinit var authManager: AuthManager

	override fun pageName(): String{
		return AppContext.getContext().getString(R.string.title_bar_modify_password)
	}
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_modify_password)

		//显示标题栏右侧按钮
		super.showTitleBtn("修改密码")

		//初始化用户信息管理器
		authManager = AuthManager()
		authManager.setOnApiResultListener(this)

		//初始化用户信息
		this.initUserInfo()

		//显示隐藏密码
		iv_modify_password_visible.setOnClickListener(PwdViewerClickListener(et_modify_password))
		iv_modify_password_again_visible.setOnClickListener(PwdViewerClickListener(et_modify_password_again))
		iv_modify_password_origin_visible.setOnClickListener(PwdViewerClickListener(et_modify_password_origin))
	}

	/**标题右侧按钮的点击事件*/
	override fun onTitleBtnClick(view: View) {
		this.modifyPassword()
	}

	/**附加控件*/
	override fun attachViewChild() {
	}

	/**
	 * 点击事件
	 */
	override fun clickEvent(id: Int, v: View) {
	}


	/**初始化用户信息*/
	private fun initUserInfo(){
		val user = SecurityUtil.getPrincipal()
		if(user != null){
			//邮箱
			et_modify_email.setText(user.email)
			//手机号
			et_modify_phone.setText(user.phone)
		}
	}

	/**调用接口保存用户信息*/
	private fun modifyPassword(){
		val user = SecurityUtil.getPrincipal()
		if(user == null){
			ToastUtils.toast("请先登录！")
			return
		}
		try {//校验信息
			user.email = et_modify_email.text.toString()
			user.phone = et_modify_phone.text.toString()

			ValidateUtil.validateEmail(user.email, "电子邮箱")
			ValidateUtil.validatePhone(user.phone, "手机号码")

			user.password = et_modify_password.text.toString()
			ValidateUtil.validateNotEmpty(user.password, "密码", "请输入密码！")
			ValidateUtil.validatePassword(user.password, "密码")

			user.passwordAgain = et_modify_password_again.text.toString()
			ValidateUtil.validateNotEmpty(user.passwordAgain, "密码确认", "请再次输入密码！")
			ValidateUtil.validatePassword(user.passwordAgain, "密码确认")

			if(user.password != user.passwordAgain){
				throw ValidateException("两次输入密码不一致！")
			}

			//原密码
			val originPassword = et_modify_password_origin.text.toString()
			ValidateUtil.validateNotEmpty(originPassword, "原密码")

			//调用接口修改密码
			authManager.modifyPassword(user, originPassword)

		} catch (e: Exception) {
			ToastUtils.toast(e.message)
		}
	}

	/**处理http请求响应结果*/
	override fun handleResult(result: ApiResult, code: Int) {
		when(code){
			Constant.HTTP_KEY_MODIFY_PASSWORD->{
				ToastUtils.toast("密码修改成功！")
			}
		}
	}


}